﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace SchEdu.Framework.DataModel
{

    public class AllocationList : List<SchEduDataSet.AllocationRow>
    {
        public AllocationList()
            : base()
        {
        }

        public AllocationList(AllocationList allocations)
            : base(allocations)
        {
        }

        public AllocationList(SchEduDataSet.AllocationRow[] allocations)
            :base(allocations)
        {
        }

        public AllocationList(SchEduDataSet.AllocationDataTable Allocations)
        {
            foreach (SchEduDataSet.AllocationRow allocation in Allocations)
            {
                Add(allocation);
            }
        }

        public int TotalProcesingTime()
        {
            int totalProcessingTime = 0;
            foreach (SchEduDataSet.AllocationRow allocation in this)
            {
                totalProcessingTime += allocation.ProcessingTime;
            }
            return totalProcessingTime;
        }

        public SchEduDataSet.AllocationRow MaxProcessingTime()
        {
            if (Count == 0)
            {
                return null;
            }
            SchEduDataSet.AllocationRow maxProcessingTime = this[0];
            foreach (SchEduDataSet.AllocationRow allocation in this)
            {
                if (allocation.ProcessingTime > maxProcessingTime.ProcessingTime)
                {
                    maxProcessingTime = allocation;
                }
            }
            return maxProcessingTime;
        }

        public int GetMakespan()
        {
            int endDate = 0;
            foreach (SchEduDataSet.AllocationRow allocation in this)
            {
                endDate = Math.Max(endDate, allocation.EndDate);
            }
            return endDate;
        }

        public SchEduDataSet.AllocationRow MinProcessingTime()
        {
            if (Count == 0)
            {
                return null;
            }
            SchEduDataSet.AllocationRow minProcessingTime = this[0];
            foreach (SchEduDataSet.AllocationRow allocation in this)
            {
                if (allocation.ProcessingTime < minProcessingTime.ProcessingTime)
                {
                    minProcessingTime = allocation;
                }
            }
            return minProcessingTime;
        }

        public SchEduDataSet.AllocationRow FindByID(int ID)
        {
            foreach (SchEduDataSet.AllocationRow allocation in this)
            {
                if (ID == allocation.ID)
                {
                    return allocation;
                }
            }
            return null;
        }

        public int GetMachineUtilization(SchEduDataSet.MachineRow machine)
        {
            int utilization = 0;
            foreach (SchEduDataSet.AllocationRow allocation in this)
            {
                if (allocation.IsMachineIDNull() == true)
                {
                    continue;
                }
                if (machine.ID != allocation.MachineID)
                {
                    continue;
                }
                utilization = utilization + (allocation.EndDate - allocation.StartDate) + 1;
            }

            return utilization;
        }

        public AllocationList GetAllocationByMachineGuid(System.Guid machineGuid)
        {
            AllocationList allocations = new AllocationList();
            foreach (SchEduDataSet.AllocationRow row in this)
            {
                if (row.MachineGuid == machineGuid)
                {
                    allocations.Add(row);
                }
            }
            return allocations;
        }

        public void SortByStartDate()
        {
            Sort(new StartDateComparer());
        }

        class StartDateComparer : Comparer<SchEduDataSet.AllocationRow>
        {

            public override int Compare(SchEduDataSet.AllocationRow x, SchEduDataSet.AllocationRow y)
            {
                if (x.StartDate < y.StartDate)
                {
                    return -1;
                }
                if (x.StartDate > y.StartDate)
                {
                    return 1;
                }
                return 0;
            }
        }


    }


}
